package xyz.mimai.media.netty.tcp.resolve;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

/**
 * @ClassName MessageUnitDecoder
 * @Description 消息解码器
 * @Author 方丈
 * @Date 2021/2/23 10:36
 * @Version 1.0.0
 **/
public class MessageUnitDecoder extends ReplayingDecoder<Void> {
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        //读取到消息长度
        int len = byteBuf.readInt();
        byte[] content = new byte[len];
        //读取指定长度的内容作为内容正文
        byteBuf.readBytes(content);
        list.add(new MessageUnit(len,content));
        System.out.println("解码器MessageUnitDecoder调用完成");
    }
}
